unit frmTableStruct;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzButton, ComCtrls, StdCtrls, frmFieldInfo, StrUtils, siComp;

type
  TEditMode = (emAdd, emEdit);
  TFormTableStruct = class(TForm)
    lvTable: TListView;
    btnAdd: TRzBitBtn;
    btnEdit: TRzBitBtn;
    btnDel: TRzBitBtn;
    Label1: TLabel;
    edtTableName: TEdit;
    btnClose: TRzBitBtn;
    btnOK: TRzBitBtn;
    siLang: TsiLang;
    procedure btnAddClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure btnDelClick(Sender: TObject);
    procedure btnRenameClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FEditMode: TEditMode;

    procedure SetEditMode(const Value: TEditMode);
    { Private declarations }
  public
    TableName: string;
    property EditMode: TEditMode read FEditMode write SetEditMode;
  end;

var
  FormTableStruct: TFormTableStruct;

implementation

uses
  frmMain;

{$R *.dfm}

{ TFormTableStruct }

procedure TFormTableStruct.btnAddClick(Sender: TObject);
var
  item: TListItem;
begin
  //  add field
  with TFormFieldInfo.Create(Application) do
  begin
    edtFieldName.Text := EmptyStr;
    cbDataType.Text := EmptyStr;
    cbPK.ItemIndex := 0;
    edtDefault.Text := EmptyStr;
    if ShowModal = mrOk then
    begin
      item := lvTable.Items.Add;
      if EditMode = emAdd then
        item.Caption := edtFieldName.Text
      else
        item.Caption := edtFieldName.Text+'+';
      item.SubItems.Add(cbDataType.Text);
      item.SubItems.Add(cbPK.Text);
      item.SubItems.Add(edtDefault.Text);
    end;
    Free;
  end;
end;

procedure TFormTableStruct.btnDelClick(Sender: TObject);
var
  tCh: Char;
begin
  //  delete field
  if lvTable.Items.Count = 0 then
    Exit;
  if lvTable.ItemIndex = -1 then
    Exit;
  tCh := lvTable.Selected.Caption[Length(lvTable.Selected.Caption)];
  if tCh = '-' then
    Exit;
  if tCh = '+' then
  begin
    lvTable.Items.Delete(lvTable.ItemIndex);
    Exit;
  end;
  if tCh = '*' then
  begin
    lvTable.Selected.Caption := LeftStr(lvTable.Selected.Caption, Length(lvTable.Selected.Caption)-1)+'-';
    Exit;
  end;
  if EditMode = emAdd then
    lvTable.Items.Delete(lvTable.ItemIndex)
  else
    lvTable.Selected.Caption := lvTable.Selected.Caption + '-';
end;

procedure TFormTableStruct.btnEditClick(Sender: TObject);
var
  tCh: Char;
begin
  if lvTable.Items.Count = 0 then
    Exit;
  if lvTable.ItemIndex = -1 then
    Exit;
  with TFormFieldInfo.Create(Application) do
  begin
    tCh := lvTable.Selected.Caption[Length(lvTable.Selected.Caption)];
    if (tCh = '*') or (tCh = '+') or (tCh = '-') then
      edtFieldName.Text := LeftStr(lvTable.Selected.Caption, Length(lvTable.Selected.Caption)-1)
    else
      edtFieldName.Text := lvTable.Selected.Caption;
    cbDataType.Text := lvTable.Selected.SubItems[0];
    cbPK.ItemIndex := cbPK.Items.IndexOf(lvTable.Selected.SubItems[1]);
    edtDefault.Text := lvTable.Selected.SubItems[2];
    if ShowModal = mrOk then
    begin
      if EditMode = emAdd then
        lvTable.Selected.Caption := edtFieldName.Text
      else
      begin
        if tCh = '+' then
          lvTable.Selected.Caption := edtFieldName.Text+'+'
        else
          lvTable.Selected.Caption := edtFieldName.Text+'*';
      end;  

      lvTable.Selected.SubItems[0] := cbDataType.Text;
      lvTable.Selected.SubItems[1] := cbPK.Text;
      lvTable.Selected.SubItems[2] := edtDefault.Text;
    end;
    Free;
  end;
end;

procedure TFormTableStruct.btnRenameClick(Sender: TObject);
begin
  //  rename table

end;

procedure TFormTableStruct.FormCreate(Sender: TObject);
begin
  siLang.ActiveLanguage := formmain.Config.Language;
end;

procedure TFormTableStruct.SetEditMode(const Value: TEditMode);
begin
  FEditMode := Value;
end;

end.
